package com.airbnb.epoxy.models;

import butterknife.BindComponent;
import com.airbnb.epoxy.*;
import ohos.agp.components.Component;

import static com.airbnb.epoxy.EpoxyAttribute.Option.DoNotHash;

/**
 * This is an example of using {@link SimpleEpoxyModel}, which is useful if you
 * don't need to do anything special in onBind. You can also instantiate {@link
 * SimpleEpoxyModel} directly instead of subclassing it if you don't need to do
 * anything in onBind.
 */
@EpoxyModelClass(layout = ResourceTable.Layout_model_color)
public abstract class ColorModel extends EpoxyModelWithHolder<ColorModel.ColorHolder> {
  @EpoxyAttribute @ColorInt int color;
  @EpoxyAttribute(DoNotHash) Component.ClickedListener clickListener;

  @Override
  public void bind(ColorHolder holder) {
    holder.cardView.setBackground(StyleElement.getStyleElement(color));
    holder.cardView.setClickedListener(clickListener);
  }

  @Override
  public void bind(ColorHolder holder, EpoxyModel<?> previouslyBoundModel) {
    // When this model changes we get a bind call with the previously bound model, so we can see
    // what changed and update accordingly.
    bind(holder);
  }


  @Override
  public void unbind(ColorHolder holder) {
    // Don't leak the click listener when this view goes back in the view pool
    holder.cardView.setClickedListener(null);
  }

  public static class ColorHolder extends BaseEpoxyHolder {
    @BindComponent(ResourceTable.Id_card_view) Component cardView;
  }
}
